package com.study.juc;

import java.util.concurrent.locks.ReentrantLock;

class Tiket {
    private int number = 50;
    //NonfairSync 默认非公平锁
//    private ReentrantLock lock = new ReentrantLock();
    private ReentrantLock lock = new ReentrantLock(true);

    public void sale() {
        lock.lock();
        try {
            if (number>0){
                System.out.println(Thread.currentThread().getName() + " :  number = " + number--);
            }
        } finally {
            lock.unlock();
        }
    }
}

public class LockDemo {
    public static void main(String[] args) {
        Tiket ticket = new Tiket();
        new Thread(()->{
            for (int i=0;i<55;i++){
                ticket.sale();
            }
        },"a").start();
        new Thread(()->{
            for (int i=0;i<55;i++){
                ticket.sale();
            }
        },"b").start();
        new Thread(()->{
            for (int i=0;i<55;i++){
                ticket.sale();
            }
        },"c").start();
    }
}
